package com.tpln.controller;

import com.alibaba.fastjson.JSON;
import com.tpln.pojo.NodeInfo;
import com.tpln.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class NodeController {
    @Autowired
    private NodeService nodeService;

    //无对应笔记页面时
    @RequestMapping("/noNodeInfo")
    @ResponseBody
    public String noNodeInfo(){
        return "<!DOCTYPE html>\n" +
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<meta charset=\"utf-8\">\n" +
                "\t\t<title></title>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<h2>你查的笔记不存在，或者该笔记已被删除，请点击下方按钮，返回主界面！！！</h2>\n" +
                "\t\t<a href=\"main.html\">返回</a>\n" +
                "\t</body>\n" +
                "</html>\n";
    }

    //笔记详情页面
    @GetMapping("nodeInfoByNodeId/{nodeId}")
    public String nodeInfoByNodeId (HttpServletRequest req,@PathVariable("nodeId") String nodeId, Model model){
        if(nodeId == null || nodeId.equals("")){
            return "redirect:/noNodeInfo";
        }

        NodeInfo nodeInfo = nodeService.selectNodeInfoByNodeId(nodeId);
        if(nodeInfo == null){
            return "redirect:/noNodeInfo";
        }

        model.addAttribute("node",nodeInfo);
        req.getSession().setAttribute("node",nodeInfo);
        return "/nodeInfo.html";
    }

    //增加笔记信息
    @RequestMapping("/addNodeInfo")
    @ResponseBody
    public String addNodeInfo(String nodeTitle, String nodeContent, HttpServletRequest req){
        HttpSession httpSession = req.getSession();
        String userId = (String) httpSession.getAttribute("userId");
        if(userId == null || userId.equals("")){
            return "请先登录，在进行增加笔记哦~";
        }
        if(nodeTitle== null || nodeContent == null || nodeTitle.equals("") || nodeContent.equals("") || nodeTitle.equals("undefined") || nodeContent.equals("undefined")){
            return "fail";
        }
        boolean result = nodeService.addNode(nodeTitle,nodeContent,userId);

        if(result){
            return "ok";
        }

        return "fail";
    }

    @PostMapping("selectNodeInfoByUserId")
    @ResponseBody
    public String selectNodeInfoByUserId(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("text/plain;charset=utf-8");
        HttpSession session = req.getSession();
        String userId = (String)session.getAttribute("userId");
        if(userId == null || userId.equals("") || userId.equals("null")){
            System.out.println("userId为空！");
            return "";
        }
        List<NodeInfo> nodeInfos = nodeService.selectNodeInfoByUserId(userId);
        if (nodeInfos.size() == 0){
            return "";
        }
        String jsonNode = JSON.toJSONString(nodeInfos);
        return jsonNode;
    }
}
